<template>
	<view>
		<view class="ul_list">
			<view 
				class="li_item"
				v-for="(item, index) in driverList"
				:key="index"
				@click="handleChooseDriver(item)"
			>
				<view class="l-wrap" @click.stop="handleGoDriverDetail(item)">
					<view class="avatar"><image :src="/^[http | https]/.test(item.avatar) ? item.avatar : (wwwImgUrl + item.avatar) " mode=""></image></view>
					<view class="name">{{item.name}}</view>
					<view class="score">
						<uni-rate size="18" :value="item.score/20" disabled></uni-rate>
					</view>
				</view>
				<uni-icons  v-if="curDriverInfo.id===item.id" type="circle-filled" size="22" color="#FFAF3C"></uni-icons>
				<uni-icons  v-else type="circle" size="22" color="#ccc"></uni-icons>
			</view>
		</view>
		
		<view class="confirm-btn-wrap">
			<view class="btn" @click="handleConfirmChooseDriver">确定</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				driverList: [],
				curDriverInfo: {},
				curDriverId: 0,
				shopId: null, // 商家id
				type: 0, // 	0.大巴 1.接送(接送暂时没有自己选司机的功能) 2.拼车 3.包车 4.租车
				page: {
					pageNum: 0,
					totalPageNum: 0,
					pageSize: 10,
				},
			};
		},
		onLoad(opt) {
			this.type = opt && opt.type
			this.curDriverId = opt && opt.curDriverId
			this.shopId = opt && opt.shopId
			this.getList()
		},
		onReachBottom() {
			let totalPage = Math.ceil(this.page.totalPageNum / this.page.pageSize)
			if (this.page.pageNum >= totalPage) return
			this.page.pageNum++
			this.getMoreBusOrderList()
		},
		methods: {
			handleChooseDriver(item) {
				this.curDriverInfo = item
			},
			handleConfirmChooseDriver() {
				this.$store.commit('upChoosedDriverObj', this.curDriverInfo)
				uni.navigateBack({delta: 1});
			},
			handleGoDriverDetail({id}) {
				uni.navigateTo({
					url: './driverDetail?id='+id
				});
			},
			// 获取司机列表
			getList() {
				this.page.pageNum = 1
				this.$http.get(this.$api.getDriverList, {
					type: this.type,
					shopId: this.shopId,
					pageSize: this.page.pageSize,
					pageNum: this.page.pageNum,
				}).then(res => {
					if (res.code === 200) {
						this.driverList = res.data
						if (this.curDriverId === 'undefined') { // 默认第一个
							this.curDriverInfo = res.data[0]
						} else {
							this.driverList.forEach(item => {
								if (item.id == this.curDriverId) {
									this.curDriverInfo = item
								}
							})
						}
					}
				})
			},
			getMoreList() {
				this.page.pageNum = 1
				this.$http.get(this.$api.getDriverList, {
					type: this.type,
					pageSize: this.page.pageSize,
					pageNum: this.page.pageNum
				}).then(res => {
					if (res.code === 200) {
						this.driverList = [...this.driverList, ...res.data]
						if (this.curDriverId === 'undefined') { // 默认第一个
							this.curDriverInfo = res.data[0]
						} else {
							this.driverList.forEach(item => {
								if (item.id == this.curDriverId) {
									this.curDriverInfo = item
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.ul_list {
	padding: 10rpx 30rpx 98rpx;
	.li_item {
		background-color: #fff;
		border-radius: 12rpx;
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 20rpx;
		padding: 0 30rpx;
		.l-wrap {
			display: flex;
			align-items: center;
			.avatar {
				width: 56rpx;
				height: 56rpx;
				border-radius: 50%;
				overflow: hidden;
				background-color: pink;
			}
			.name {
				margin: 0 30rpx;
				font-size: 26rpx;
			}
			.score {
				margin-top: 15rpx;
			}
		}
	}
}
.confirm-btn-wrap {
	height: 98rpx;
	background-color: #fff;
	position: fixed;
	left: 0;
	right: 0;
	bottom: 0;
	.btn {
		height: 72rpx;
		line-height: 72rpx;
		text-align: center;
		width: 690rpx;
		margin:13rpx auto;
		background-color: $blue;
		color: #fff;
		font-size: 32rpx;
		border-radius: 12rpx;
	}
}
</style>
